home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume14 / unzip-3.1 / part03 < prev    next >
Encoding:
Text File  |  1990-09-20  |  43.7 KB  |  1,343 lines

  1. Newsgroups: comp.sources.misc
  2. From: kirsch@usasoc.soc.mil (David Kirschbaum)
  3. Subject: v14i106: Portable UnZIP 3.1 (part 3 of 3)
  4. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5.  
  6. Posting-number: Volume 14, Issue 106
  7. Submitted-by: kirsch@usasoc.soc.mil (David Kirschbaum)
  8. Archive-name: unzip-3.1/part03
  9.  
  10. #! /bin/sh
  11. # This is a shell archive.  Remove anything before this line, then unpack
  12. # it by saving it into a file and typing "sh file".  To overwrite existing
  13. # files, type "sh file -c".  You can also feed this as standard input via
  14. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  15. # will see the following message at the end:
  16. #        "End of archive 3 (of 3)."
  17. # Contents:  history.310 unzip.c
  18. # Wrapped by kirsch@usasoc.soc.mil on Fri Aug 31 11:37:53 1990
  19. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  20. if test -f 'history.310' -a "${1}" != "-c" ; then 
  21.   echo shar: Will not clobber existing file \"'history.310'\"
  22. else
  23. echo shar: Extracting \"'history.310'\" \(18493 characters\)
  24. sed "s/^X//" >'history.310' <<'END_OF_FILE'
  25. X********************************
  26. Xv3.10, 16 Aug 90
  27. X- No significant changes.  Two new systems added to Makefile.
  28. X- Dif file required for Coherent installation in coherent.msg.
  29. X- Dif files required for Atari (Turbo C v2.0) in atari.zip
  30. X  (atari.hdr is msg header for initial uuencoded transfer).
  31. X  These dif files are for an earlier version (v3.06?) and may need
  32. X  some tweaking to patch smoothly.
  33. X  They (and the Coherent patches) have NOT been installed.     
  34. X
  35. XVersion 3.10 will be the current "fielded" version until further notice.
  36. XNo patches to earlier versions will be accepted.
  37. X
  38. XThe Info-ZIP workgroup are currently working on a new beta-test version
  39. Xthat may or may not involve the following "features":
  40. X  - default lowercasing of extracted member file names
  41. X  - IFDEFs for incremental vs. new "monster" compile
  42. X  - Integration of the Atari (Turbo C) patches.
  43. X  - Use of central directory for member listing and more.
  44. X  - Use of central directory "created by" byte to make "intelligent"
  45. X    guesses as to filename and/or text conversion.
  46. X
  47. X(No there were no versions 3.08 or 3.09: we went right from v3.07 (last
  48. Xbeta test) to v3.10.)
  49. X
  50. X********************************
  51. Xv3.07, 30 Jul 90
  52. X- No changes. Just a new addition to the Makefile (see below).
  53. X  Changing internal version number to v3.07 and updating archives
  54. X  at SIMTEL20.
  55. X
  56. XThings have been stable for a LONG time, so it's time to send a tar
  57. Xto the uunet boys for archiving over there.
  58. X
  59. X>today I picked up version 3.05 of unzip and got it running OK in very
  60. X>little time on out IBM RT 6150 under AIX 2.2.1. The only thing necessary
  61. X>was to add a line:
  62. X>rtaix: _16zmem # IBM RT 6150 under AIX 2.2.1
  63. X>in the Makefile, and <make rtaix> does the rest.
  64. X>
  65. X>Erik-Jan Vens
  66. X>Interdisciplinary Center for the development of Computer Coaches and
  67. X>Expertsystems
  68. X>University of Groningen, the Neths.
  69. X>Vens@Rug.Nl
  70. X
  71. X*********************************
  72. Xv3.06, 26 May 90
  73. X  - Patches to fix a bug that sneaked in to the header parsing in unzip.c.
  74. X  - New system (68000-type), plus small tweaks to Makefile.
  75. X  - Minor changes to "usage" display.
  76. X
  77. X*********************************
  78. Xv3.05, 25 May 90
  79. X  - Bill Davidsen tweaked mapname.c to solve some problems.
  80. X    Then he cleaned up Makefile to solve the identical-system problems.
  81. X  - He also did a hack on unzip.c to provide "ask before overwrite".
  82. X    (Bill's a busy lad, ne?)
  83. X  - James Dugal provided a patch for skip_to_signature to solve a problem
  84. X    with his Pyramid system (once again, unsigned char-related).
  85. X    He also added a small eof-trapping routine during signature scan.
  86. X  - Several requests to break up unzip.c into smaller pieces (but by
  87. X    function or logically, not just arbitrarily by line numbers or whatever).
  88. X    We can do that!  Now have separate files for file I/O (file_io.c),
  89. X    the different unzip algorithms (unimplod.c, [unreduce.c, unshrink.c),
  90. X    and one big unzip.h with lots of defines, etc.
  91. X    Makes unzip.c smaller, anyway.
  92. X    crc32.h is gone now .. its two lines are incorporated in unzip.h
  93. X
  94. X*********************************
  95. Xv3.04, 22 May 90
  96. X  - Commented out ae_buf() in ascec.c (no longer used) with Warner
  97. X    Losh's changes to CR/LF conversion.
  98. X  - Trying out Warner's patch to enable unzip to work with self-extracting
  99. X    ".EXE" files (via bruteforce feeling about for the first file header).
  100. X    He warns this byte-by-byte read will be sloooow .. yeah, sure, we could
  101. X    do something flash with a big buffer read .. but who needs it?  How
  102. X    often will we be processing self-extracting file headers, after all? 
  103. X  - Bo Kullmar (all the way from Sweden!) had the nicest patch for the
  104. X    changed help display, so his shall be implemented.  (How's instant world
  105. X    fame feel, Bo?)
  106. X  - Got two submissions of the same kinda fix for the duplicate system
  107. X    requirement problem (crudely fixed in v3.03).  Nice, elegant, except
  108. X    it don't work on my Vax BSD system!  Ah well, I installed the fix anyway
  109. X    in Makefile.  If you have problems, examine the vaxbsd portion to see
  110. X    if you might have to drop back to brute force.
  111. X
  112. X*******************************************
  113. X- v3.03
  114. X   Reconciliation of earlier patches from Warner Losh, plus fix to
  115. X   make stat() work.
  116. X
  117. X   New Makefile with separate commands for each different system
  118. X   (too many makes didn't like things like
  119. X     wombat:    # thanks to Joe Isuzu
  120. X     sun4:    #
  121. X        cc etc. etc.
  122. X   )
  123. X
  124. X   unzip will NOT work on self-extracting .EXE files (a problem with how
  125. X   it looks for the first file header).
  126. X
  127. XThe file unzp303s.arc (source)  will be distributed via EMail to Info-ZIP
  128. Xmembers.  (It'll include the latest Makefile, all source files, plus this
  129. Xmessage.)  No significant changes to documentation, so no separate distribution
  130. Xof that stuff via a separate unzp303d.arc.
  131. X
  132. XAs usual, EVERYTHING will be in unzip303.arc and unzip303.tar-Z at
  133. XSIMTEL20.
  134. X****************************************
  135. X- v3.02 continued
  136. X
  137. XDate: Tue, 15 May 90 22:19:39 MDT
  138. XFrom: imp@Solbourne.COM (Warner Losh)
  139. XTo: info-zip@wsmr-simtel20.army.mil
  140. XSubject: Patch for .exe problem.
  141. X
  142. XHere is a patch for the problem that was reported about not being able
  143. Xto extract a zip file named foo.exe.  I don't know of any problems
  144. Xwith this patch.  It should work on any system that knows about the
  145. Xstat command.  If you suspect this patch is causing you problems, then
  146. Xjust add -DREALOLDSTUFF to your CFLAGS line in the Makefile and it
  147. Xwill compile the old way.
  148. X
  149. XWarner Losh             imp@Solbourne.COM
  150. X
  151. X[Applied via patch to v3.02.  Actual patch is in patchexe.pch file.
  152. X Since v3.02 hasn't REALLY been fielded yet,
  153. X no change to version number.
  154. X
  155. XThis patch, with its use of the stat() function, didn't want to work on
  156. Xmy BSD 4.3 system (problems with stat() always returning a -1, not being
  157. Xable to define a struct stat variable, etc.).
  158. XReplaced the "file-existence" testing with the access() function
  159. Xand it worked just fine!  You can now direct unzip to any target file at all.
  160. Xunzip will check for that file's existence, and if not found, will add a
  161. X".zip" and try again.  That oughtta take care of LEVELS.ZIP, selfextr.exe,
  162. Xand so forth. 
  163. X
  164. XMy system is now working perfectly, using various combinations of the
  165. X-a and -m switch.  No CRC errors, file end-of-line conversion is working
  166. Xwell, etc.
  167. X
  168. X19 May 90
  169. X David Kirschbaum
  170. X Toad Hall
  171. X]
  172. X***********************************************
  173. X-v 3.02
  174. X
  175. XDate: Wed, 9 May 90 11:27:39 MDT
  176. XTo: info-zip@wsmr-simtel20.army.mil
  177. XSubject: Patch for the -a CRC problem.
  178. XFrom: imp@Solbourne.COM (Warner Losh)
  179. X
  180. XGreetings...
  181. X
  182. Xsince it looks like David is going away for a while, here are the
  183. Xcontext diffs to 3.0 to make it quit bitching about CRC problems when
  184. Xit unpacks incorrectly.  I didn't patch the existing code.  I rewrote
  185. Xit so that all writes go though FlushBuffer which calls WriteBuffer
  186. Xwhich might call dos2unix to map <CR><LF> to just <LF>.  Note:  I
  187. Xthink I broke EBCDIC in the process.  Lemme know what you think of
  188. Xthese patches.  They worked for me a dozen times on this sun4 I have.
  189. XI'd be interested in knowing where else they work.
  190. X
  191. XOh yah, if the last character of the buffer is <CR>, then weird things
  192. Xwill happen (more than likely it will do nothing).  There is a bug
  193. Xthat in what I sent to David that causes a buffer overflow (dos2unix
  194. Xlooks at too many character) in reading (but not in writing.  The one
  195. Xline fix is to add i++ where you see it in this context diff.
  196. X
  197. XWarner Losh
  198. Ximp@Solbourne.COM
  199. X[Applied to v3.01, producing v3.02.
  200. X Patch itself is in the file losh.pat
  201. X David Kirschbaum
  202. X Toad Hall
  203. X]
  204. X
  205. X***************************************  
  206. X- v3.01  
  207. X
  208. XFrom: Bo Kullmar <bk@kullmar.se>
  209. XAs far as I understand there is a little bug in version 3.0 with option -a.
  210. XThe resulting file will be one character to long compared to a file that is
  211. Xconverted separately. It is due to the fact that the first element in the
  212. Xbuffer is 0 and the ocount goes from 1 to ...
  213. X
  214. XThis does not solve the bug with reporting wrong CRC.
  215. X
  216. XThe CR conversion must only be performed if there is a CR+LF. Not if there
  217. Xonly is a CR. This patch fixes this also.
  218. X
  219. XI noticed that the text for the -a option is worg. It does not change CR
  220. Xto space, it removes CR.
  221. X
  222. XToad Hall: Added the HP 9000/HP-UX makerules to Makefile
  223. X           Consolidated several system makerules to clean up a little.
  224. X****************************************** 
  225. X- v3.0
  226. X   Moderate friendly pressure from Keith Petersen, Keeper of the Archives
  227. X   at SIMTEL20, to "get this sucker out the door" (or words to that effect).
  228. X   SHOT.  OUT.
  229. X   (And thanks to all the contributors of Info-ZIP!  Nice work, people!)
  230. X   David Kirschbaum
  231. X   Coordinator (?) Info-ZIP
  232. X******************************************
  233. X *
  234. X * v2.0j, 25 Apr 90
  235. X * After MUCH discussion, adding a "-m" switch to map extracted member
  236. X * filenames to lower case (if upper case).
  237. X * (Scan for "mapped_name" function and "mflag" switch.)
  238. X * Recently revealed problem re MS-DOS PKZIP'ed files and paths:
  239. X * Now stripping all paths from member filenames.
  240. X * (All this stuff in mapname.c)
  241. X * David Kirschbaum
  242. X * Toad Hall
  243. X *
  244. X * v2.0i, around 20 Apr 90
  245. X * Changes to Makefile only.  Distributed via EMail, SIMTEL archives updated.
  246. X************************************
  247. X * 13 Apr 90 v2.0h
  248. X
  249. XGeneral synopsis of changes from v2.0g to 2.0h:
  250. X
  251. XFrom Bill Davidsen
  252. X   (davidsen@crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
  253. X
  254. XFixed Makefile up.
  255. X
  256. X  Also, the inclusion of types.h for O_BINARY is wrong... this is DOS
  257. Xstuff, not needed in UNIX. I put #ifndef UNIX around it to keep it from
  258. Xcausing trouble. All of the code which uses it is also ifndef'd, so this
  259. Xcan't break anything.
  260. X[Looks like it's MTS stuff also.]
  261. X
  262. XRemoved a duplicate define.
  263. X
  264. XFixed the -a switch:
  265. X
  266. XAs implemented it totally breaks C programs being unzipped from DOS to
  267. XUNIX. The option changes the return to a blank instead of deleting it.
  268. XSince C requires that continuation be noted with a \ *as the last
  269. Xcharacter on a line* this breaks the program.
  270. X
  271. X  Some editors don't strip or ignore trailing blanks, and when
  272. Xreformatting paragraphs this results in two blanks between words, or
  273. Xeven the lines not being joined.
  274. X
  275. X  In short, this is a hack which can badly mangle the files on which
  276. Xit's used. I will look to see if the logic will support simply deleting
  277. Xthe returns. Proper logic should be to delete a return only if it is
  278. Xfollowed by a newline, since any other logic breaks embedded returns
  279. Xused for overstrike.
  280. X
  281. X...
  282. X
  283. X  Well, it turned out not to be too hard to fix the -a option to do the
  284. Xright thing and simply remove the offending returns. I didn't look too
  285. Xclosely, but I'm not sure this ever worked, since the test for return
  286. Xwas made after the character had been translated into EBCDIC. Perhaps
  287. Xthe table lookup turned the return into a space.
  288. X
  289. X  After testing on sun, convex, encore, stellar, and xenix, here are the
  290. Xfinal diffs I had to make to get 20g working. Would someone please test
  291. Xthem on a EBCDIC machine?
  292. X
  293. X[Diffs were removed, but they affected:
  294. XMakefile:
  295. Xunzip.c:
  296. Xascebc.c:
  297. X]
  298. X
  299. X  Still doesn't seem to work on 64 bit machines.
  300. X
  301. X*******************
  302. X
  303. XAnd from Forrest Gehrke, for his VAX SysV system:
  304. X
  305. XOk, here's the story for VAX SysV:
  306. X
  307. XSysV chokes on an #include David added, to wit:  [NOT me!]
  308. X  #include <sys/file.h>  // v2.0g Chitra says MTS needs this for O_BINARY //
  309. X
  310. XSo, that line will have to be handled as an #ifdef MTS conditional
  311. Xand a Makefile rule change added to have that compiler conditionally
  312. Xinclude that file.
  313. X
  314. X[We've made it an #ifdef MTS.
  315. X However, I'm waiting for an authentic MTS hacker to produce and authenticate
  316. Xthe requirement with a new Makefile.
  317. X]
  318. X
  319. XAs to the Makefile, I have found that the recipe provided
  320. Xfor "encore" does the job.  I have added an identical 
  321. Xlayout with the exception of a "system" label called
  322. X   vaxsysV
  323. XA copy of that Makefile is included herewith:  [Eaten in the mail]
  324. X
  325. X*********************
  326. X
  327. XAbove comments consolidated from a number of Info-ZIP messages in prep for
  328. Xthe v2.0h release.
  329. X
  330. XDavid Kirschbaum
  331. Xkirsch@usasoc.soc.mil, 13 Apr 90
  332. X
  333. X*************************************************
  334. X* 7 Apr 90 v2.0g
  335. X * - Incorporated Allan Bjorklund's -a buffer bug fix.
  336. X *   The old contents of outbuf are used by unzip to extract the next
  337. X *   block of data.  The fix involved setting up an auxillary buffer to hold
  338. X *   the translated material so that outbuf could be left alone.
  339. X *   Just scan for "outout" to see where he (and I) made the changes.
  340. X *   (I tightened up his code a little in FlushOutput(), whether to
  341. X *   allocate outout at all, when/how to output from outout ).
  342. X * - Found and fixed the couple of bogus UWORDs that sneaked in during a
  343. X *   global search-and-replace (like "UWORDp").
  344. X * - Took a couple Sun-related error (mine) out of Makefile.
  345. X *
  346. X * 03/28/90 v2.0f
  347. X * - Using a REAL complicated Makefile that sets makerules for all sorts
  348. X *   of different systems.
  349. X *   See Makefile to see if this'll compile on your system.
  350. X * - Added Allan Bjorklund's patch to enable EBCDIC/EOL conversion
  351. X *   during output to stdout.
  352. X * - Changed the typedef "word" to UWORD throughout.  (Known conflict on
  353. X *   Crays.)
  354. X * - Changed "memcpy()" and "memset()" throughout to "zmemcpy()" and
  355. X *   "zmemset()".  This is for the situation where some systems' library
  356. X *   memcpy() and/or memset() don't work properly .. or don't exist at all.
  357. X *   A compile define ("-DZMEM") enables use of our own functions
  358. X *   (in zmemcpy.c and zmemset.c).  Members suggested we not use the
  359. X *   ANSI-defined function names in that case.
  360. X *   If you have NOT used the "-DZMEM", no problem .. the library's
  361. X *   memcpy() and memset() will be used.
  362. X *
  363. X * 03/23/90 v2.0e
  364. X * - Allan Bjorklund's tweaks: (allanb@us.cc.umich.edu)
  365. X * I've also added another switch to the program for ASCII->EBCDIC
  366. X * conversion.  If your wondering why I would do such a thing, it
  367. X * is because I manage two archives sites. One on the SUN I'm working on
  368. X * now (terminator) and one on an IBM 3090-600E (um{ub}mts.cc.umich.edu).
  369. X * After I had gotten the code for the ascii->ebcdic working, I rearranged
  370. X * the defines so the for people not using ebcdic on their system could
  371. X * use it to convert 0x0D to 0x20.  I find it real annoying to see ^M
  372. X * at the end of the lines of text while in vi.  The option can be invoked
  373. X * with -a.
  374. X *
  375. X * Toad Hall Notes:
  376. X * Alan's changes include a new cmdline "-a" flag, a new "ascebc.c" file,
  377. X * and a new "Makefile".  Define "EBCDIC" to enable the ebcdic conversion.
  378. X * (And I'd be REAL careful if I were you about just which files you
  379. X * used that "-a" switch for CR/LF -> LF conversion in a Unix system.)
  380. X *
  381. X * Re other portability problems, turns out the problem is in the memcpy()
  382. X * routines (along with general confusion about other compile defines.
  383. X * We'll have a separate doc ref how to compile on different systems.
  384. X * David Kirschbaum
  385. X *
  386. X * 03/20/90  D. Kirschbaum v2.0d
  387. X *      - Found a bug down in FillBitBuff() (introduced in v2.0c).
  388. X *        Fixed.  While I was hunting that, I changed a bunch of
  389. X *        long variables to unsigned longs (AKA longint) just to
  390. X *        be nice and consistent.
  391. X *        The structures seem to work fine now with the NOTINT16
  392. X *        compiler flag enabled.  Strangely, on my VAX 11-780,
  393. X *        the first structure in each pair (the one with the longs)
  394. X *        is two bytes longer than the byte-defined structure.
  395. X *        Your mileage may vary (enable DEBUG to see what I mean).
  396. X *      - davidsen@crdos1.crd.ge.com cleaned up a bunch of the
  397. X *        ifdef's, and re-enabled the LONGI macros.  He also
  398. X *        picked up an unnecessary address override.
  399. X *
  400. X * 03/19/90  D. Kirschbaum v2.0c
  401. X *              - Many problems with structures (since different machines
  402. X *                align words and longs differently, have different sizes
  403. X *                of words and longs, etc.
  404. X *                Fixing by reading our structures into a working
  405. X *                structure that's ALL bytes, and then processing each
  406. X *                and every bloody word and long into a separate structure
  407. X *                properly defined as words, longs, etc.
  408. X *                Define "NOTINT16" to make this happen.
  409. X *                (With that above define, it STILL compiles and runs fine
  410. X *                with Turbo C.)
  411. X *                NO idea what'll happen with "bigendian" processors!
  412. X *
  413. X * 03/16/90  D. Kirschbaum v2.0b Hacked back to Turbo C v2.0 and BSD 4.3
  414. X *              - Added a bunch of Turbo- and BSD 4.3-unique (I guess)
  415. X *                #include <>'s.
  416. X *              - Prototyped everything to reduce Turbo C warnings.
  417. X *              - Removing the Daylight Savings Time adjustment for file
  418. X *                times.  (They're throwing the file time off by an hour
  419. X *                on my BSD system.)
  420. X *              - Changed target .zip filename ".zip" appending
  421. X *                so it can deal with paths like "..\subdir\filename".
  422. X *                (Original would catch the first "." and not add the zip.)
  423. X *              - Including .TC and .PRJ files for Turbo C compiles.
  424. X *              - Including Makefile.BSD for Unix BSD compiles.
  425. X *                (Rename to Makefile and "make unzip".)
  426. X *              MUCH better version than the horrible I've been hacking at
  427. X *              for the past couple of months.  Everyone throw that old
  428. X *              unzip23 away!
  429. X *              Compiles just fine on my DOS 3.1 clone, Turbo C v2.0,
  430. X *              and on my Vax 11-780, BSD 4.3.
  431. X *              David Kirschbaum
  432. X *              Toad Hall
  433. X *
  434. X * 12/14/89  C. Mascott  2.0a   adapt for UNIX
  435. X *              ifdef HIGH_LOW swap bytes in time, date, CRC,
  436. X *                version needed, bit flag
  437. X *              implement true s-f trees instead of table
  438. X *              don't pre-allocate output file space
  439. X *              implement -t, -v, member file specs
  440. X *              buffer all input
  441. X *              change hsize_array_integer to short
  442. X *              overlap storage used by different comp. methods
  443. X *              speed up unImplode
  444. X *              use insertion sort in SortLengths
  445. X *              define zipfile header structs in a way that
  446. X *                avoids structure padding on 32-bit machines
  447. X *              fix "Bad CRC" msg: good/bad CRCs were swapped
  448. X *              check for write error on output file
  449. X *              added by John Cowan <cowan@magpie.masa.com>:
  450. X *              support -c option to expand to console
  451. X *              use stderr for messages
  452. X *              allow lowercase component name specs
  453. X *
  454. END_OF_FILE
  455. if test 18493 -ne `wc -c <'history.310'`; then
  456.     echo shar: \"'history.310'\" unpacked with wrong size!
  457. fi
  458. # end of 'history.310'
  459. fi
  460. if test -f 'unzip.c' -a "${1}" != "-c" ; then 
  461.   echo shar: Will not clobber existing file \"'unzip.c'\"
  462. else
  463. echo shar: Extracting \"'unzip.c'\" \(22987 characters\)
  464. sed "s/^X//" >'unzip.c' <<'END_OF_FILE'
  465. X/*
  466. X * Copyright 1989 Samuel H. Smith;  All rights reserved
  467. X *
  468. X * Do not distribute modified versions without my permission.
  469. X * Do not remove or alter this notice or any other copyright notice.
  470. X * If you use this in your own program you must distribute source code.
  471. X * Do not use any of this in a commercial product.
  472. X *
  473. X */
  474. X
  475. X/*
  476. X * UnZip - A simple zipfile extract utility
  477. X *
  478. X * Compile-time definitions:
  479. X * See the Makefile for details, explanations, and all the current
  480. X * system makerules.
  481. X *
  482. X * If you have to add a new one for YOUR system, please forward the
  483. X * new Makefile to kirsch@usasoc.soc.mil for distribution.
  484. X * Be SURE to give FULL details on your system (hardware, OS, versions,
  485. X * processor, whatever) that made it unique.
  486. X *
  487. X * REVISION HISTORY : See history.307 (or whatever current version is)
  488. X *
  489. X */
  490. X
  491. X#include "unzip.h"    /* v3.05 a BUNCH of ifdefs, etc.
  492. X             * split out to reduce file size.
  493. X             * David Kirschbaum
  494. X             */
  495. X
  496. Xchar *fnames[2] = { /* default filenames vector */
  497. X    "*",
  498. X    NULL
  499. X};
  500. Xchar **fnv = &fnames[0];
  501. X
  502. Xint tflag;      /* -t: test */
  503. Xint vflag;      /* -v: view directory */
  504. Xint cflag;      /* -c: output to stdout (JC) */
  505. Xint aflag;      /* -a: do ascii to ebcdic translation 2.0f */
  506. X                /*     OR <cr><nl> to <nl> conversion  */
  507. Xint mflag;    /* -m: map member filenames to lower case v2.0j */
  508. Xint CR_flag = 0; /* When last char of buffer == CR */
  509. X
  510. Xint members;
  511. Xlongint csize;
  512. Xlongint ucsize;
  513. Xlongint tot_csize;
  514. Xlongint tot_ucsize;
  515. X
  516. X
  517. X/* ----------------------------------------------------------- */
  518. X/*
  519. X * shrink/reduce working storage
  520. X *
  521. X */
  522. X
  523. Xint factor;
  524. X/* really need only 256, but prefix_of, which shares the same
  525. X   storage, is just over 16K */
  526. Xbyte followers[257][64];    /* also lzw prefix_of, s-f lit_nodes */
  527. Xbyte Slen[256];
  528. X
  529. Xtypedef short hsize_array_integer[hsize+1]; /* was used for prefix_of */
  530. Xtypedef byte hsize_array_byte[hsize+1];
  531. X
  532. Xshort *prefix_of = (short *) followers; /* share reduce/shrink storage */
  533. Xhsize_array_byte suffix_of;     /* also s-f length_nodes */
  534. Xhsize_array_byte stack;         /* also s-f distance_nodes */
  535. X
  536. Xint codesize;
  537. Xint maxcode;
  538. Xint free_ent;
  539. Xint maxcodemax;
  540. Xint offset;
  541. Xint sizex;
  542. X
  543. X/* Code now begins ( .. once more into the Valley of Death.. ) */
  544. X
  545. X
  546. X#ifdef NOTINT16     /* v2.0c */
  547. XUWORD makeword(b)
  548. Xbyte * b;
  549. X /* convert Intel style 'short' integer to non-Intel non-16-bit
  550. X  * host format
  551. X  */
  552. X{
  553. X/*
  554. X    return  ( ((UWORD) (b[1]) << 8)
  555. X            | (UWORD) (b[0])
  556. X            );
  557. X*/
  558. X    return  ( ( b[1] << 8)
  559. X            | b[0]
  560. X            );
  561. X}
  562. X
  563. Xlongint makelong(sig)
  564. Xbyte *sig;
  565. X /* convert intel style 'long' variable to non-Intel non-16-bit
  566. X  * host format
  567. X  */
  568. X{
  569. X    return ( ((longint) sig[3]) << 24)
  570. X          + ( ((longint) sig[2]) << 16)
  571. X          + ( ((longint) sig[1]) << 8)
  572. X          +   ((longint)  sig[0]) ;
  573. X}
  574. X#endif  /* NOTINT16 */
  575. X
  576. X#ifdef HIGH_LOW
  577. X
  578. Xvoid swap_bytes(wordp)
  579. XUWORD *wordp;
  580. X /* convert Intel style 'short int' variable to host format */
  581. X{
  582. X    char *charp = (char *) wordp;
  583. X    char temp;
  584. X
  585. X    temp = charp[0];
  586. X    charp[0] = charp[1];
  587. X    charp[1] = temp;
  588. X}
  589. X
  590. Xvoid swap_lbytes(longp)
  591. Xlongint *longp;
  592. X /* convert intel style 'long' variable to host format */
  593. X{
  594. X    char *charp = (char *) longp;
  595. X    char temp[4];
  596. X
  597. X    temp[3] = charp[0];
  598. X    temp[2] = charp[1];
  599. X    temp[1] = charp[2];
  600. X    temp[0] = charp[3];
  601. X
  602. X    charp[0] = temp[0];
  603. X    charp[1] = temp[1];
  604. X    charp[2] = temp[2];
  605. X    charp[3] = temp[3];
  606. X}
  607. X
  608. X#endif /* HIGH_LOW */
  609. X/* ----------------------------------------------------------- */
  610. X
  611. X#include "file_io.c"        /* v3.05 file-related vars, functions */
  612. X
  613. X#include "unreduce.c"        /* v3.05 */
  614. X
  615. X#include "unshrink.c"        /* v3.05 */
  616. X
  617. X#include "unimplod.c"        /* v3.05 */
  618. X
  619. X
  620. X/* ---------------------------------------------------------- */
  621. X
  622. X/*
  623. X Length  Method   Size  Ratio   Date    Time   CRC-32    Name
  624. X ------  ------   ----- -----   ----    ----   ------    ----
  625. X  44004  Implode  13041  71%  11-02-89  19:34  88420727  DIFF3.C
  626. X */
  627. X
  628. Xvoid dir_member()
  629. X{
  630. X    char *method;
  631. X    int ratio;
  632. X    int yr, mo, dy, hh, mm;
  633. X
  634. X    yr = (((lrec.last_mod_file_date >> 9) & 0x7f) + 80);
  635. X    mo = ((lrec.last_mod_file_date >> 5) & 0x0f);
  636. X    dy = (lrec.last_mod_file_date & 0x1f);
  637. X
  638. X    hh = ((lrec.last_mod_file_time >> 11) & 0x1f);
  639. X    mm = ((lrec.last_mod_file_time >> 5) & 0x3f);
  640. X
  641. X    switch (lrec.compression_method)  {
  642. X    case 0:
  643. X        method = "Stored";
  644. X        break;
  645. X    case 1:
  646. X        method = "Shrunk";
  647. X        break;
  648. X    case 2:
  649. X    case 3:
  650. X    case 4:
  651. X    case 5:
  652. X        method = "Reduced";
  653. X        break;
  654. X    case 6:
  655. X        method = "Implode";
  656. X        break;
  657. X    }
  658. X
  659. X    if (ucsize != 0)  {
  660. X        ratio = (int) ((1000L * (ucsize - csize)) / ucsize);
  661. X        if ((ratio % 10) >= 5)
  662. X            ratio += 10;
  663. X    }
  664. X    else
  665. X        ratio = 0;  /* can .zip contain 0-size file? */
  666. X
  667. X#ifdef NOTINT16     /* v2.0c */
  668. X    printf("%7ld  %-7s%7ld %3d%%  %02d-%02d-%02d  %02d:%02d  \
  669. X%08lx  %s\n", ucsize, method, csize,
  670. X        ratio / 10, mo, dy, yr, hh, mm,
  671. X        lrec.crc32, filename);
  672. X#else   /* !NOTINT16 */
  673. X    printf("%7ld  %-7s%7ld %3d%%  %02d-%02d-%02d  %02d:%02d  \
  674. X%08lx  %s\n", ucsize, method, csize,
  675. X        ratio / 10, mo, dy, yr, hh, mm,
  676. X        LONGI(lrec.crc32), filename);
  677. X#endif  /* NOTINT16 */
  678. X    tot_ucsize += ucsize;
  679. X    tot_csize += csize;
  680. X    ++members;
  681. X}
  682. X
  683. X/* ---------------------------------------------------------- */
  684. X
  685. Xvoid skip_member()
  686. X{
  687. X    register long pos;
  688. X    long endbuf;
  689. X    int offset;
  690. X
  691. X    endbuf = lseek(zipfd, 0L, SEEK_CUR);    /* 1st byte beyond inbuf */
  692. X    pos = endbuf - incnt;                   /* 1st compressed byte */
  693. X    pos += csize;                           /* next header signature */
  694. X    if (pos < endbuf)  {
  695. X        incnt -= csize;
  696. X        inptr += csize;
  697. X    }
  698. X    else  {
  699. X        offset = pos % BSIZE;               /* offset within block */
  700. X        pos = (pos / BSIZE) * BSIZE;        /* block start */
  701. X        lseek(zipfd, pos, SEEK_SET);
  702. X        incnt = read(zipfd, inbuf, INBUFSIZ);
  703. X        incnt -= offset;
  704. X        inptr = inbuf + offset;
  705. X    }
  706. X}
  707. X
  708. X/* ---------------------------------------------------------- */
  709. X
  710. Xvoid extract_member()
  711. X{
  712. X        UWORD     b;
  713. X/* for test reasons */
  714. X
  715. X    bits_left = 0;
  716. X    bitbuf = 0;
  717. X    outpos = 0L;
  718. X    outcnt = 0;
  719. X    outptr = outbuf;
  720. X    zipeof = 0;
  721. X    crc32val = 0xFFFFFFFFL;
  722. X
  723. X    zmemset(outbuf, 0, OUTBUFSIZ);
  724. X    if (aflag)                          /* if we have scratchpad.. v2.0g */
  725. X        zmemset(outout, 0, OUTBUFSIZ);  /* ..clear it out v2.0g */
  726. X      
  727. X    if (tflag)
  728. X        fprintf(stderr, "Testing: %-12s ", filename);
  729. X    else {
  730. X        if(!mapped_name())    /* member name conversion failed  v2.0j */
  731. X        exit(1);        /* choke and die v2.0j */
  732. X
  733. X        /* create the output file with READ and WRITE permissions */
  734. X        if (create_output_file())
  735. X            return; /* was exit(1); */
  736. X    }
  737. X    switch (lrec.compression_method) {
  738. X
  739. X    case 0: {   /* stored */
  740. X            if (!tflag)
  741. X                fprintf(stderr, " Extracting: %-12s ", filename);
  742. X            if (cflag) fprintf(stderr, "\n");
  743. X            while (ReadByte(&b))
  744. X                OUTB(b);
  745. X        }
  746. X        break;
  747. X
  748. X    case 1: {       /* shrunk */
  749. X            if (!tflag)
  750. X                fprintf(stderr, "UnShrinking: %-12s ", filename);
  751. X            if (cflag) fprintf(stderr, "\n");
  752. X            unShrink();
  753. X        }
  754. X        break;
  755. X
  756. X    case 2:
  757. X    case 3:
  758. X    case 4:
  759. X    case 5: {
  760. X            if (!tflag)
  761. X                fprintf(stderr, "  Expanding: %-12s ", filename);
  762. X            if (cflag) fprintf(stderr, "\n");
  763. X            unReduce();
  764. X        }
  765. X        break;
  766. X
  767. X    case 6: {
  768. X            if (!tflag)
  769. X                fprintf(stderr, "  Exploding: %-12s ", filename);
  770. X            if (cflag) fprintf(stderr, "\n");
  771. X            unImplode();
  772. X        }
  773. X        break;
  774. X
  775. X    default:
  776. X        fprintf(stderr, "Unknown compression method.");
  777. X    }
  778. X
  779. X    /* write the last partial buffer, if any */
  780. X    FlushOutput ();
  781. X
  782. X    if (!tflag)  {
  783. X#ifndef UNIX
  784. X        /* set output file date and time */
  785. X        set_file_time();
  786. X        close(outfd);
  787. X#else
  788. X        close(outfd);
  789. X        /* set output file date and time */
  790. X        set_file_time();
  791. X#endif
  792. X    }
  793. X
  794. X    crc32val = ~crc32val;
  795. X#ifdef NOTINT16     /* v2.0c */
  796. X    if (crc32val != lrec.crc32)
  797. X        fprintf(stderr, " Bad CRC %08lx  (should be %08lx)", crc32val,
  798. X            lrec.crc32);
  799. X#else   /* !NOTINT16 */
  800. X    if (crc32val != LONGI(lrec.crc32))
  801. X        fprintf(stderr, " Bad CRC %08lx  (should be %08lx)", crc32val,
  802. X            LONGI(lrec.crc32));
  803. X#endif  /* NOTINT16 */
  804. X
  805. X    else if (tflag)
  806. X        fprintf(stderr, " OK");
  807. X
  808. X    fprintf(stderr, "\n");
  809. X}
  810. X
  811. X
  812. X/* ---------------------------------------------------------- */
  813. X
  814. Xvoid get_string(len, s)
  815. Xint len;
  816. Xchar *s;
  817. X{
  818. X    readbuf(zipfd, s, len);
  819. X    s[len] = 0;
  820. X
  821. X#ifdef EBCDIC           /* translate the filename to ebcdic */
  822. X    a_to_e( s );        /* A.B.  03/21/90                   */
  823. X#endif
  824. X}
  825. X
  826. X
  827. X/* ---------------------------------------------------------- */
  828. X
  829. Xvoid process_local_file_header(fnamev)
  830. Xchar **fnamev;
  831. X{
  832. X    int extracted;
  833. X#ifdef NOTINT16     /* v2.0c */
  834. X    local_byte_header brec;
  835. X#endif
  836. X
  837. X#ifndef NOTINT16    /* v2.0c */
  838. X    readbuf(zipfd, (char *) &lrec, sizeof(lrec));   /* v2.0b */
  839. X#else   /* NOTINT16 */
  840. X    readbuf(zipfd, (char *) &brec, sizeof(brec));
  841. X
  842. X    lrec.version_needed_to_extract =
  843. X        makeword(brec.version_needed_to_extract);
  844. X    lrec.general_purpose_bit_flag =
  845. X        makeword(brec.general_purpose_bit_flag);
  846. X    lrec.compression_method =
  847. X        makeword(brec.compression_method);
  848. X    lrec.last_mod_file_time =
  849. X        makeword(brec.last_mod_file_time);
  850. X    lrec.last_mod_file_date =
  851. X        makeword(brec.last_mod_file_date);
  852. X    lrec.crc32 =
  853. X        makelong(brec.crc32);
  854. X    lrec.compressed_size =
  855. X        makelong(brec.compressed_size);
  856. X    lrec.uncompressed_size =
  857. X        makelong(brec.uncompressed_size);
  858. X    lrec.filename_length =
  859. X        makeword(brec.filename_length);
  860. X    lrec.extra_field_length =
  861. X        makeword(brec.extra_field_length);
  862. X#endif  /* NOTINT16 */
  863. X
  864. X#ifdef HIGH_LOW
  865. X    swap_bytes(&lrec.filename_length);
  866. X    swap_bytes(&lrec.extra_field_length);
  867. X    swap_lbytes(LONGIP(lrec.compressed_size));
  868. X    swap_lbytes(LONGIP(lrec.uncompressed_size));
  869. X    swap_bytes(&lrec.compression_method);
  870. X    swap_bytes(&lrec.version_needed_to_extract);
  871. X    swap_bytes(&lrec.general_purpose_bit_flag);
  872. X    swap_bytes(&lrec.last_mod_file_time);
  873. X    swap_bytes(&lrec.last_mod_file_date);
  874. X    swap_lbytes(LONGIP(lrec.crc32));
  875. X#endif  /* HIGH_LOW */
  876. X
  877. X#ifdef NOTINT16     /* v2.0c */
  878. X    csize = lrec.compressed_size;
  879. X    ucsize = lrec.uncompressed_size;
  880. X#else   /* !NOTINT16 */
  881. X    csize = LONGI(lrec.compressed_size);
  882. X    ucsize = LONGI(lrec.uncompressed_size);
  883. X#endif  /* NOTINT16 */
  884. X
  885. X    get_string(lrec.filename_length, filename);
  886. X    get_string(lrec.extra_field_length, extra);
  887. X
  888. X    extracted = 0;
  889. X    for (--fnamev; *++fnamev; )  {
  890. X        if (match(filename, *fnamev))  {
  891. X            if (vflag)
  892. X                dir_member();
  893. X            else  {
  894. X                extract_member();
  895. X                extracted = 1;
  896. X            }
  897. X            break;
  898. X        }
  899. X    }
  900. X    if (!extracted)
  901. X        skip_member();
  902. X}
  903. X
  904. X
  905. X/* ---------------------------------------------------------- */
  906. X
  907. Xvoid process_central_file_header()
  908. X{
  909. X    central_directory_file_header rec;
  910. X    char filename[STRSIZ];
  911. X    char extra[STRSIZ];
  912. X/*  char comment[STRSIZ]; v2.0b using global comment so we can display it */
  913. X
  914. X#ifdef NOTINT16     /* v2.0c */
  915. X    central_directory_byte_header byterec;
  916. X#endif
  917. X
  918. X#ifndef NOTINT16    /* v2.0c */
  919. X    readbuf(zipfd, (char *) &rec, sizeof(rec)); /* v2.0b */
  920. X#else   /* NOTINT16 */
  921. X    readbuf(zipfd, (char *) &byterec, sizeof(byterec) );        /* v2.0c */
  922. X
  923. X    rec.version_made_by =
  924. X        makeword(byterec.version_made_by);
  925. X    rec.version_needed_to_extract =
  926. X        makeword(byterec.version_needed_to_extract);
  927. X    rec.general_purpose_bit_flag =
  928. X        makeword(byterec.general_purpose_bit_flag);
  929. X    rec.compression_method =
  930. X        makeword(byterec.compression_method);
  931. X    rec.last_mod_file_time =
  932. X        makeword(byterec.last_mod_file_time);
  933. X    rec.last_mod_file_date =
  934. X        makeword(byterec.last_mod_file_date);
  935. X    rec.crc32 =
  936. X        makelong(byterec.crc32);
  937. X    rec.compressed_size =
  938. X        makelong(byterec.compressed_size);
  939. X    rec.uncompressed_size =
  940. X        makelong(byterec.uncompressed_size);
  941. X    rec.filename_length =
  942. X        makeword(byterec.filename_length);
  943. X    rec.extra_field_length =
  944. X        makeword(byterec.extra_field_length);
  945. X    rec.file_comment_length =
  946. X        makeword(byterec.file_comment_length);
  947. X    rec.disk_number_start =
  948. X        makeword(byterec.disk_number_start);
  949. X    rec.internal_file_attributes =
  950. X        makeword(byterec.internal_file_attributes);
  951. X    rec.external_file_attributes =
  952. X        makeword(byterec.external_file_attributes);
  953. X    rec.relative_offset_local_header =
  954. X        makelong(byterec.relative_offset_local_header);
  955. X#endif  /* NOTINT16 */
  956. X
  957. X#ifdef HIGH_LOW
  958. X    swap_bytes(&rec.filename_length);
  959. X    swap_bytes(&rec.extra_field_length);
  960. X    swap_bytes(&rec.file_comment_length);
  961. X#endif
  962. X
  963. X    get_string(rec.filename_length, filename);
  964. X    get_string(rec.extra_field_length, extra);
  965. X    get_string(rec.file_comment_length, comment);
  966. X}
  967. X
  968. X
  969. X/* ---------------------------------------------------------- */
  970. X
  971. Xvoid process_end_central_dir()
  972. X{
  973. X    end_central_dir_record rec;
  974. X/*  char comment[STRSIZ]; v2.0b made global */
  975. X
  976. X#ifdef NOTINT16     /* v2.0c */
  977. X    end_central_byte_record byterec;
  978. X#endif
  979. X
  980. X#ifndef NOTINT16    /* v2.0c */
  981. X    readbuf(zipfd, (char *) &rec, sizeof(rec)); /* v2.0b */
  982. X#else   /* NOTINT16 */
  983. X    readbuf(zipfd, (char *) &byterec, sizeof(byterec) );
  984. X
  985. X    rec.number_this_disk =
  986. X        makeword(byterec.number_this_disk);
  987. X    rec.number_disk_with_start_central_directory =
  988. X        makeword(byterec.number_disk_with_start_central_directory);
  989. X    rec.total_entries_central_dir_on_this_disk =
  990. X        makeword(byterec.total_entries_central_dir_on_this_disk);
  991. X    rec.total_entries_central_dir =
  992. X        makeword(byterec.total_entries_central_dir);
  993. X    rec.size_central_directory =
  994. X        makelong(byterec.size_central_directory);
  995. X    rec.offset_start_central_directory =
  996. X        makelong(byterec.offset_start_central_directory);
  997. X    rec.zipfile_comment_length =
  998. X        makeword(byterec.zipfile_comment_length);
  999. X#endif  /* NOTINT16 */
  1000. X
  1001. X#ifdef HIGH_LOW
  1002. X    swap_bytes(&rec.zipfile_comment_length);
  1003. X#endif
  1004. X
  1005. X    /* There seems to be no limit to the zipfile
  1006. X       comment length.  Some zipfiles have comments
  1007. X       longer than 256 bytes.  Currently no use is
  1008. X       made of the comment anyway.
  1009. X     */
  1010. X/* #if 0
  1011. X * v2.0b Enabling comment display
  1012. X */
  1013. X    get_string(rec.zipfile_comment_length, comment);
  1014. X/* #endif */
  1015. X}
  1016. X
  1017. X
  1018. X/* ---------------------------------------------------------- */
  1019. X
  1020. Xvoid process_headers()
  1021. X{
  1022. X    int ratio;
  1023. X    long sig;
  1024. X
  1025. X#ifdef NOTINT16     /* v2.0c */
  1026. X    byte sigbyte[4];
  1027. X#endif
  1028. X
  1029. X    if (vflag)  {
  1030. X        members = 0;
  1031. X        tot_ucsize = tot_csize = 0;
  1032. X        printf("\n Length  Method   Size  Ratio   Date    Time   \
  1033. XCRC-32    Name\n ------  ------   ----- -----   ----    ----   ------    \
  1034. X----\n");
  1035. X    }
  1036. X
  1037. X    while (1) {
  1038. X#ifdef NOTINT16     /* v2.0c */
  1039. X    if (readbuf(zipfd, (char *) sigbyte, 4) != 4)
  1040. X#else   /* !NOTINT16 */
  1041. X    if (readbuf(zipfd, (char *) &sig, sizeof(sig)) != sizeof(sig))
  1042. X#endif  /* NOTINT16 */
  1043. X        return;
  1044. X
  1045. X#ifdef NOTINT16     /* v2.0c */
  1046. X        sig = makelong(sigbyte);
  1047. X#endif
  1048. X
  1049. X#ifdef HIGH_LOW
  1050. X        swap_lbytes(&sig);
  1051. X#endif
  1052. X
  1053. X        if (sig == LOCAL_FILE_HEADER_SIGNATURE)
  1054. X            process_local_file_header(fnv);
  1055. X        else if (sig == CENTRAL_FILE_HEADER_SIGNATURE)
  1056. X            process_central_file_header();
  1057. X        else if (sig == END_CENTRAL_DIR_SIGNATURE) {
  1058. X            process_end_central_dir();
  1059. X            break;
  1060. X        }
  1061. X        else {
  1062. X            fprintf(stderr, "Invalid Zipfile Header\n");
  1063. X            return;
  1064. X        }
  1065. X    }
  1066. X    if (vflag)  {
  1067. X        if (tot_ucsize != 0)  {
  1068. X            ratio = (int) ((1000L * (tot_ucsize-tot_csize))
  1069. X                    / tot_ucsize);
  1070. X            if ((ratio % 10) >= 5)
  1071. X                ratio += 10;
  1072. X        }
  1073. X        else
  1074. X            ratio = 0;
  1075. X        printf(" ------          ------  \
  1076. X---                             -------\n\
  1077. X%7ld         %7ld %3d%%                             %7d\n",
  1078. X        tot_ucsize, tot_csize, ratio / 10, members);
  1079. X
  1080. X        if( comment[0] )                /* v2.0b */
  1081. X            printf("%s\n",comment);     /* v2.0b */
  1082. X    }
  1083. X}
  1084. X
  1085. X
  1086. X/* ---------------------------------------------------------- */
  1087. X/* v3.04 Patch to enable processing of self-extracting ".EXE"
  1088. X * (and other) files that might have weird junk before the first
  1089. X * actual file member.
  1090. X * I don't THINK anyone'll have problems with this .. but just in case,
  1091. X * you can disable the entire mess by enabling the "NOSKIP" ifdef.
  1092. X * (up near code top).
  1093. X * Thanks to Warner Losh for this patch.
  1094. X */
  1095. X#ifndef NOSKIP
  1096. Xvoid skip_to_signature()
  1097. X{
  1098. X    static char pk[] = "PK";
  1099. X    int i, nread;
  1100. X    unsigned char ch;
  1101. X    extern int errno;
  1102. X    
  1103. X    errno = 0;            /* Be sure we start with 0 */
  1104. X    do {
  1105. X        /*
  1106. X         * Search for "PK"
  1107. X         */
  1108. X        i = 0;
  1109. X        while ((nread = read (zipfd, &ch, 1)) && i < 2) {
  1110. X            if (ch == pk[i]) {
  1111. X                i++;
  1112. X            }
  1113. X            else {
  1114. X                if (ch == pk[0])
  1115. X                    i = 1;
  1116. X                else
  1117. X                    i = 0;
  1118. X            }
  1119. X        }
  1120. X    if (errno || nread==0) {    /* read err or EOF */
  1121. X        fprintf(stderr, "Unable to find a valid header signature. Aborting.\n");
  1122. X        exit (2);
  1123. X    }
  1124. X    } while (ch > 20);
  1125. X
  1126. X    /*
  1127. X     * We have now read 3 characters too many, so we backup.
  1128. X     */
  1129. X    lseek (zipfd, -3L, SEEK_CUR);
  1130. X}
  1131. X#endif        /* NOSKIP */
  1132. X
  1133. X
  1134. Xvoid process_zipfile()
  1135. X{
  1136. X    /*
  1137. X     * open the zipfile for reading and in BINARY mode to prevent cr/lf
  1138. X     * translation, which would corrupt the bitstreams
  1139. X     */
  1140. X
  1141. X    if (open_input_file())
  1142. X        exit(1);
  1143. X
  1144. X#ifndef NOSKIP        /* v3.03 */
  1145. X    skip_to_signature();    /* read up to first "PK%" v3.03 */
  1146. X#endif
  1147. X    process_headers();
  1148. X
  1149. X    close(zipfd);
  1150. X}
  1151. X
  1152. X/* ---------------------------------------------------------- */
  1153. X
  1154. Xvoid usage()        /* v2.0j */
  1155. X{
  1156. X
  1157. X#ifdef EBCDIC                              /* A.B. 03/20/90   */
  1158. X  char *astring = "-a  ascii to ebcdic conversion";
  1159. X#else
  1160. X  char *astring = "-a  convert to unix textfile format (CR LF => LF)";    /* v3.04 */
  1161. X#endif
  1162. X
  1163. Xfprintf(stderr, "\n%s\nCourtesy of:  S.H.Smith  and  The Tool Shop BBS,  (602) 279-2673.\n\n",VERSION);
  1164. Xfprintf(stderr, "Usage:  unzip [-tcamv] file[.zip] [filespec...]\n\
  1165. X  -t  test member files\n\
  1166. X  -c  output to stdout\n\
  1167. X  %s\n\
  1168. X  -m  map extracted filenames to lowercase\n\
  1169. X  -v  view directory\n",astring);
  1170. X    exit(1);
  1171. X}
  1172. X
  1173. X/* ---------------------------------------------------------- */
  1174. X
  1175. X/*
  1176. X * main program
  1177. X *
  1178. X */
  1179. X
  1180. Xvoid main(argc, argv)
  1181. Xint argc;
  1182. Xchar **argv;
  1183. X{
  1184. X    char *s;
  1185. X    int c;
  1186. X    struct stat statbuf;        /* v3.03 */
  1187. X    
  1188. X#ifdef DEBUG_STRUC                      /* v2.0e */
  1189. Xprintf("local_file_header size: %X\n",
  1190. X    sizeof(struct local_file_header) );
  1191. Xprintf("local_byte_header size: %X\n",
  1192. X    sizeof(struct local_byte_header) );
  1193. X
  1194. Xprintf("central directory header size: %X\n",
  1195. X    sizeof(struct central_directory_file_header) );
  1196. Xprintf("central directory byte header size: %X\n",
  1197. X    sizeof(struct central_directory_byte_header) );
  1198. X
  1199. Xprintf("end central dir record size: %X\n",
  1200. X    sizeof(struct end_central_dir_record) );
  1201. Xprintf("end central dir byte record size: %X\n",
  1202. X    sizeof(struct end_central_byte_record) );
  1203. X#endif
  1204. X
  1205. X    while (--argc > 0 && (*++argv)[0] == '-')  {
  1206. X        s = argv[0] + 1;
  1207. X#ifndef __TURBOC__
  1208. X        while (c = *s++)  {
  1209. X#else
  1210. X        while ((c = *s++) != '\0') {      /* v2.0b */
  1211. X#endif
  1212. X
  1213. X/* Something is SERIOUSLY wrong with my host's tolower() function! */
  1214. X/* #ifndef BSD */
  1215. X/*            switch (tolower(c))  { */
  1216. X/* #else */
  1217. X            switch(c) {
  1218. X/* #endif */
  1219. X            case ('T'):
  1220. X            case ('t'):
  1221. X                ++tflag;
  1222. X                break;
  1223. X            case ('V'):
  1224. X            case ('v'):
  1225. X                ++vflag;
  1226. X                break;
  1227. X            case ('C'):
  1228. X            case ('c'):
  1229. X                ++cflag;
  1230. X#ifdef EBCDIC
  1231. X                ++aflag;       /* This is so you can read it on the screen */
  1232. X#endif                         /*  A.B.  03/24/90                          */
  1233. X                break;
  1234. X            case ('A'):
  1235. X            case ('a'):
  1236. X                ++aflag;
  1237. X                break;
  1238. X        case ('M'):        /* map filename flag v2.0j */
  1239. X        case ('m'):
  1240. X        ++mflag;
  1241. X        break;
  1242. X            default:
  1243. X                usage();
  1244. X                break;
  1245. X            }
  1246. X        }
  1247. X    }
  1248. X/*  I removed the filter for the a and c flags so they could be used together */
  1249. X/*  Especially on EBCDIC based systems where you would want to read the text  */
  1250. X/*  on the screen.  Allan Bjorklund.  03/24/90                                */
  1251. X
  1252. X    if ((tflag && vflag) || (tflag && cflag) || (vflag && cflag) ||
  1253. X        (tflag && aflag) || (aflag && vflag))
  1254. X
  1255. X    {
  1256. X        fprintf(stderr, "only one of -t, -c, -a, or -v\n");
  1257. X        exit(1);
  1258. X    }
  1259. X    if (argc-- == 0)
  1260. X        usage();
  1261. X
  1262. X    /* .ZIP default if none provided by user */
  1263. X    strcpy(zipfn, *argv++);
  1264. X/* v2.0b This doesn't permit paths like "..\dir\filename" */
  1265. X
  1266. X#ifdef REALOLDSTUFF        /* v3.02 */
  1267. X#ifdef OLDSTUF
  1268. X    if (strchr(zipfn, '.') == NULL)
  1269. X        strcat(zipfn, ZSUFX);
  1270. X#else    /* v2.0b New code */
  1271. X
  1272. X    c = strlen(zipfn);
  1273. X    if ( (c < 5)                             /* less than x.zip */
  1274. X      || (strcmp (&zipfn[c-4], ZSUFX) != 0)) /* v2.0b type doesn't
  1275. X                                              * match */
  1276. X        strcat (zipfn, ZSUFX);
  1277. X#endif
  1278. X#else                /* v3.02 */
  1279. X      /*
  1280. X       * OK, first check to see if the name is as given.  If it is
  1281. X       * found as given, then we don't need to bother adding the
  1282. X       * ZSUFX.  If it isn't found, then add the ZSUFX.  We don't
  1283. X       * check to see if this results in a good name, but that check
  1284. X       * will be done later.
  1285. X       */
  1286. X    if(stat (zipfn, &statbuf))        /* v3.02 */
  1287. X        strcat (zipfn, ZSUFX);        /* v3.02 */
  1288. X
  1289. X#endif    /* not REALOLDSTUF */
  1290. X      
  1291. X    /* if any member file specs on command line, set filename
  1292. X       pointer to point to them. */
  1293. X
  1294. X    if (argc != 0)
  1295. X        fnv = argv;
  1296. X
  1297. X        /* allocate i/o buffers */
  1298. X    inbuf = (byte *) (malloc(INBUFSIZ));
  1299. X    outbuf = (byte *) (malloc(OUTBUFSIZ));
  1300. X
  1301. X    /* v2.0g Hacked Allan's code.  No need allocating an ascebc
  1302. X     * scratch buffer unless we're doing translation.
  1303. X     */
  1304. X
  1305. X    if(aflag)                           /* we need an ascebc scratch v2.0g */
  1306. X        outout = (byte *) (malloc(OUTBUFSIZ)); /* ..so allocate it v2.0g */
  1307. X    else
  1308. X        outout = outbuf;                /* just point to outbuf v2.0g */
  1309. X
  1310. X    if ((inbuf == NULL) || (outbuf == NULL) || (outout == NULL)) {  /* v2.0g */
  1311. X        fprintf(stderr, "Can't allocate buffers!\n");
  1312. X        exit(1);
  1313. X    }
  1314. X
  1315. X    /* do the job... */
  1316. X    process_zipfile();
  1317. X    exit(0);
  1318. X}
  1319. X
  1320. END_OF_FILE
  1321. if test 22987 -ne `wc -c <'unzip.c'`; then
  1322.     echo shar: \"'unzip.c'\" unpacked with wrong size!
  1323. fi
  1324. # end of 'unzip.c'
  1325. fi
  1326. echo shar: End of archive 3 \(of 3\).
  1327. cp /dev/null ark3isdone
  1328. MISSING=""
  1329. for I in 1 2 3 ; do
  1330.     if test ! -f ark${I}isdone ; then
  1331.     MISSING="${MISSING} ${I}"
  1332.     fi
  1333. done
  1334. if test "${MISSING}" = "" ; then
  1335.     echo You have unpacked all 3 archives.
  1336.     rm -f ark[1-9]isdone
  1337. else
  1338.     echo You still need to unpack the following archives:
  1339.     echo "        " ${MISSING}
  1340. fi
  1341. ##  End of shell archive.
  1342. exit 0
  1343.